/******************************************************************
 * @version      : 1.0
 * @Author       : zhl
 * @Date         : 2023-05-17 13:53:07
 * @LastEditors  : zhl
 * @E-Mail       : zhll168@163.com
 * @Description  : 业务描述
 ******************************************************************/
#ifndef __JSON_TRANSLATOR_H__
#define __JSON_TRANSLATOR_H__

#include <string>
#include <iostream>
#include <unordered_map>
#include <typeinfo>
#include <iostream>
#include <unistd.h>
#include <cstddef>
#include <sys/timeb.h>
#include <regex>
#include "json.hpp"
#include "common.h"
#include "CLogger.h"
#include "upgrade_data.h"


using json = nlohmann::json;



template<typename T>
void json_to_obj(const json& j, std::string field, T& obj){
    if(field.empty()){
        return ;
    }
    if(j.contains(field)){
        j.at(field).get_to(obj);
    }
}

void to_json(json& j, const UPGRADE_REQ_DATA& obj) {
    j = json{
        {"upgradeId",obj.upgrade_id}, 
        {"target",obj.target}, 
        {"upgradeFile", obj.uri},
        {"userName",obj.username}, 
        {"password", obj.password}
    };
}

void from_json(const json& j, UPGRADE_REQ_DATA& obj) {
    json_to_obj<int64_t>(j, "upgradeId", obj.upgrade_id);
    json_to_obj<int64_t>(j, "target", obj.target);
    json_to_obj<std::string>(j, "upgradeFile", obj.uri);
    json_to_obj<std::string>(j, "userName", obj.username);
    json_to_obj<std::string>(j, "password", obj.password);

}

void to_json(json& j, const UPGRADE_REP_DATA& obj) {
    j = json{
        {"code",obj.code}, 
        {"upgradeId",obj.upgrade_id}, 
        {"msg", obj.msg}
    };
}

void from_json(const json& j, UPGRADE_REP_DATA& obj) {
    json_to_obj<int>(j, "code", obj.code);
    json_to_obj<int64_t>(j, "upgradeId", obj.upgrade_id);
    json_to_obj<std::string>(j, "msg", obj.msg);

}

void to_json(json& j, const VERSION_DATA& obj) {
    j = json{
        {"code",obj.code}, 
        {"target", obj.target}, 
        {"ver", obj.ver}
    };
}

void from_json(const json& j, VERSION_DATA& obj) {
    json_to_obj<int>(j, "code", obj.code);
    json_to_obj<int>(j, "target", obj.target);
    json_to_obj<std::string>(j, "ver", obj.ver);

}

void to_json(json& j, const std::list<VERSION_DATA>& objs) {
    for(auto ver = objs.begin(); ver != objs.end(); ver++){
        j.push_back(*ver);
    }
}

void from_json(const json& j, std::list<VERSION_DATA>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        VERSION_DATA ver = *jj;
        objs.push_back(ver);
    }
}

void to_json(json& j, const UPGRADE_REQ_MSG_TYPE &obj) {
    j["commandId"] = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;
    j["params"]       = obj.data;    
}

void from_json(const json& j, UPGRADE_REQ_MSG_TYPE &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<UPGRADE_REQ_DATA>(j, "params", obj.data);
}

void to_json(json& j, const UPGRADE_REP_MSG_TYPE &obj) {
    j["commandId"] = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;
    j["params"]       = obj.data;    
}

void from_json(const json& j, UPGRADE_REP_MSG_TYPE &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<UPGRADE_REP_DATA>(j, "params", obj.data);
}

void to_json(json& j, const ONLINE_MSG_TYPE &obj) {
    j["commandId"] = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;
    j["params"]["versionInfo"]       = obj.data;    
}

void from_json(const json& j, ONLINE_MSG_TYPE &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    if(j.contains("params")){
        json_to_obj<std::list<VERSION_DATA>>(j["params"], "versionInfo", obj.data);
    }
    
}

void to_json(json& j, const MSG_HEADER &obj) {
    j["commandId"] = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;   
}

void from_json(const json& j, MSG_HEADER &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);    
}

void to_json(json& j, const GET_LOG_PARAMS &obj) {
    j["type"]         = obj.type;   
}

void from_json(const json& j, GET_LOG_PARAMS &obj) {
    json_to_obj<int>(j, "type", obj.type);
}

void to_json(json& j, const GET_LOG_REQ &obj) {
    j["commandId"]  = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;  
    j["params"]     = obj.data; 
}

void from_json(const json& j, GET_LOG_REQ &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<GET_LOG_PARAMS>(j, "params", obj.data);
}

void to_json(json& j, const REPORT_LOG_DATA &obj) {
    j["fileName"]       = obj.file_name;
    j["fileType"]       = obj.file_type;
    j["content"]        = obj.content;
    j["packetNum"]      = obj.packet_num;
    j["packetTotalNum"] = obj.packet_total_num;
}

void from_json(const json& j, REPORT_LOG_DATA &obj) {
    json_to_obj<std::string>(j, "fileName", obj.file_name);
    json_to_obj<std::string>(j, "fileType", obj.file_type);
    json_to_obj<std::string>(j, "content", obj.content);
    json_to_obj<int>(j, "packetNum", obj.packet_num);
    json_to_obj<int>(j, "packetTotalNum", obj.packet_total_num);
}

void to_json(json& j, const REPORT_LOG_REQ_OR_REP &obj) {
    j["commandId"]  = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;  
    j["params"]     = obj.data; 
}

void from_json(const json& j, REPORT_LOG_REQ_OR_REP &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<REPORT_LOG_DATA>(j, "params", obj.data);
}

void to_json(json& j, const COMMAND_PARAMS &obj) {
    j["cmd"]         = obj.cmd;   
}

void from_json(const json& j, COMMAND_PARAMS &obj) {
    json_to_obj<std::string>(j, "cmd", obj.cmd);
}

void to_json(json& j, const EXECUTE_CMD_TYPE &obj) {
    j["commandId"]  = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;  
    j["params"]     = obj.data; 
}

void from_json(const json& j, EXECUTE_CMD_TYPE &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<COMMAND_PARAMS>(j, "params", obj.data);
}

void to_json(json& j, const COMMAND_RESULT &obj) {
    j["result"]         = obj.result;   
}

void from_json(const json& j, COMMAND_RESULT &obj) {
    json_to_obj<std::string>(j, "result", obj.result);
}

void to_json(json& j, const EXECUTE_CMD_RESULT &obj) {
    j["commandId"]  = obj.command_id;
    j["timestamp"]  = obj.timestamp;
    j["command"]    = obj.command;
    j["commandType"] = obj.command_type;
    j["sn"]         = obj.sn;  
    j["params"]     = obj.data; 
}

void from_json(const json& j, EXECUTE_CMD_RESULT &obj) {
    json_to_obj<int64_t>(j, "commandId", obj.command_id);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::string>(j, "command", obj.command);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<COMMAND_RESULT>(j, "params", obj.data);
}

#endif /*__JSON_TRANSLATOR_H__*/